package code.tools.commponents
{
	import flash.display.BitmapData;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;

	/**
	 * 这是一个将文字转换为位图的类
	 * @author liangqian
	 */
	public class TextBitmapData
	{
		/**
		 * 用于突出中间文字内容的黑色光晕滤镜
		 */
		private static var blackGlowFilter:GlowFilter = new GlowFilter(0 , 1 , 2 , 2 , 3);// old new GlowFilter(0,1,4,4,2)

		static public const TEXT_SHADOW_FILTER:Array  = [blackGlowFilter];

		/**
		 * 绘制文字的格式
		 */
		private static var _style:uint;

		/**
		 * 缓存池
		 * key => bitmapData;
		 * value => (_text + 'x' + _color);
		 */
		private static var cachePool:Dictionary       = new Dictionary(true);

		/**
		 * 用于生成文字的textfield
		 */
		private static var tf:TextField;

		/**
		 * 字体格式
		 */
		private static const tft:TextFormat           = new TextFormat();
//		/**
//		 * 嵌入的字体格式
//		 */
//		static public const EMBED_FONT_NAME:String='EnglishFont';
//
//		//		 [Embed(source="../../assets/Helvetica_LT_55_Roman.ttf", fontName="EnglishFont", unicodeRange="U+0020-U+007e", embedAsCFF="false", mimeType="application/x-font")]
//		//		[Embed(source="../../assets/Helvetica_LT_57_Condensed_Oblique.ttf", fontName="EnglishFont", unicodeRange="U+0020-U+007e", embedAsCFF="false", mimeType="application/x-font")]
//		//		[Embed(source="../../assets/Helvetica_LT_67_Medium_Condensed_Oblique.ttf", fontName="EnglishFont", unicodeRange="U+0020-U+007e", embedAsCFF="false", mimeType="application/x-font")]
//		[Embed(source="../../assets/Helvetica_LT_77_Bold_Condensed_Oblique.ttf", fontName="EnglishFont", unicodeRange="U+0020-U+007e", embedAsCFF="false", mimeType="application/x-font")]
//		static private var EnglishFontArc:Class;
//		private static const ENGLISH_TEXT_FORMAT:TextFormat=new TextFormat(EMBED_FONT_NAME);

		/**
		 * 将输入的字符串变成位图, 返回绘制了文字的 bitmap data对象，这个bitmapdata对象是自动回收的，使用方不应该主动回收
		 * @param text 需要绘制的文字
		 * @param color 文字颜色
		 * @param baseBmp 绘制文字的底图
		 * @return
		 */
		public static function textToBitmap(text:String , color:uint = 0xffffff , fontSize:int = 12):BitmapData
		{
			if (text == "" || text == null)
			{
				return null;
			}

			var key:String   = text + 'x' + color.toString(16) + 'x' + fontSize;
			var bmd:BitmapData = getCacheBitmapData(key);
			if (bmd == null)
			{
				tf.filters = TEXT_SHADOW_FILTER;
				tft.color = color;
				tft.size = fontSize;
				tf.defaultTextFormat = tft;
				tf.text = text;
				var w:int = tf.width;
				var h:int = tf.height;
				bmd = new BitmapData(w , h , true , 0);
				bmd.draw(tf);
				cachePool[bmd] = key;
			}
			return bmd;
		}

		public static function htmlToBitmap(text:String , color:uint = 0xffffff , fontSize:int = 12):BitmapData
		{
			if (text == "" || text == null)
				return null;
			tft.color = color;
			tft.size = fontSize;
			tf.defaultTextFormat = tft;
			tf.htmlText = text;
			var bmd:BitmapData = new BitmapData(tf.width , tf.height , true , 0);
			bmd.draw(tf);
			return bmd;
		}

//		public static function EmbedFontToBitmap(text:int , color:uint = 0xffffff , fontSize:int = 12):BitmapData
//		{
//			ENGLISH_TEXT_FORMAT.color = color;
//			ENGLISH_TEXT_FORMAT.size = fontSize;
//			tf.defaultTextFormat = ENGLISH_TEXT_FORMAT;
//			tf.embedFonts=true;
//			tf.text = text+'';
//			var bmd:BitmapData = new BitmapData(tf.width , tf.height , true , 0);
//			bmd.draw(tf);
//			tf.embedFonts=false;
//			return bmd;
//		}

		private static function getCacheBitmapData(key:String):BitmapData
		{
			for (var i:* in cachePool)
			{
				if (cachePool[i] == key)
					return i;
			}
			return null;
		}

		/* static init */
		tf = new TextField();
		tf.border = false;
		tf.background = false;
		tf.selectable = false;
		tf.mouseEnabled = false;
		tf.tabEnabled = false;
		tf.autoSize = 'left';
		tf.wordWrap = false
		tf.multiline = false;

	}
}
